package com._21cn._front.product.page.wap;

import com._21cn._front.GenerateHandler;
import com._21cn._front.product.page.label.CarLabel;
import com._21cn.cms.common.ErrorCode;
import com._21cn.cms.common.obj.ResponseObj;
import com._21cn.cms.template.entity.Template;
import com._21cn.cms.template.service.TemplateService;
import com._21cn.cms.template.utils.TemplateHelper;
import com._21cn.cms.template.utils.label.ToolsLabel;
import com._21cn.framework.http.HttpRequestInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/3.
 */
@Component("wapPledgeCarPageList")
public class WapPledgeCarPageList implements GenerateHandler {
    private static Integer templateId = 29;

    private static String templateContent = "";

    @Autowired
    private TemplateService templateService;

    @Autowired
    private CarLabel carLabel;

    @Autowired
    private ToolsLabel toolsLabel;

    @Override
    public ResponseObj generate(HttpServletRequest request) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
        Integer pageSize = reqInfo.getIntParameter("pageSize", 15);
        Integer brandId = reqInfo.getIntParameter("brandId", 0);
        Integer seriesId = reqInfo.getIntParameter("seriesId", 0);
        Integer otherId = reqInfo.getIntParameter("otherId", 0);
        Integer gtPrice = reqInfo.getIntParameter("gtPrice", 0);
        Integer ltPrice = reqInfo.getIntParameter("ltPrice", 0);
        String sortRank = reqInfo.getParameter("sortRank", "createTimeDesc");
        String currentUrl = reqInfo.getUrl();
        ResponseObj responseObj = new ResponseObj();

        try {
            Map maps = new HashMap();
            maps.put("carLabel",    this.carLabel);
            maps.put("toolsLabel",  this.toolsLabel);

            Template template = templateService.getById(templateId);
            templateContent = template.getContent();

            String templateStr = templateContent.replace("{pageNo}", pageNo.toString())
                    .replace("{pageSize}", pageSize.toString())
                    .replace("{brandId}", brandId.toString())
                    .replace("{seriesId}", seriesId.toString())
                    .replace("{otherId}", otherId.toString())
                    .replace("{gtPrice}", gtPrice.toString())
                    .replace("{ltPrice}", ltPrice.toString())
                    .replace("{sortRank}", sortRank.toString())
                    .replace("{currentUrl}", currentUrl);

            String content = TemplateHelper.evaluateTemplate(templateStr, maps);

            responseObj.setErrorCode(ErrorCode.SUCCESS_CODE);
            responseObj.setMessage(content);
        }
        catch (Exception e) {
            responseObj.setErrorCode(ErrorCode.ERROR_SERVER_EXCEPTION);
            responseObj.setMessage("生成页面异常");
        }

        return responseObj;
    }
}
